/**
 * 接口模板管理器
 * 提供常用的接口模板，如登录、分页列表、文件上传等
 */

// 登录接口模板
const loginTemplate = {
  method: 'POST',
  url: '/api/auth/login',
  parameters: [
    {
      name: 'username',
      type: 'string',
      required: true,
      description: '用户名'
    },
    {
      name: 'password',
      type: 'string',
      required: true,
      description: '密码'
    }
  ],
  response: [
    {
      name: 'code',
      type: 'number',
      description: '状态码'
    },
    {
      name: 'message',
      type: 'string',
      description: '响应消息'
    },
    {
      name: 'data',
      type: 'object',
      description: '响应数据'
    },
    {
      name: 'data.token',
      type: 'string',
      description: '认证令牌'
    },
    {
      name: 'data.userInfo',
      type: 'object',
      description: '用户信息'
    },
    {
      name: 'data.userInfo.id',
      type: 'number',
      description: '用户ID'
    },
    {
      name: 'data.userInfo.name',
      type: 'string',
      description: '用户姓名'
    }
  ]
}

// 分页列表接口模板
const paginatedListTemplate = {
  method: 'GET',
  url: '/api/items',
  parameters: [
    {
      name: 'page',
      type: 'number',
      required: false,
      description: '页码'
    },
    {
      name: 'pageSize',
      type: 'number',
      required: false,
      description: '每页条数'
    },
    {
      name: 'keyword',
      type: 'string',
      required: false,
      description: '搜索关键词'
    }
  ],
  response: [
    {
      name: 'code',
      type: 'number',
      description: '状态码'
    },
    {
      name: 'message',
      type: 'string',
      description: '响应消息'
    },
    {
      name: 'data',
      type: 'object',
      description: '响应数据'
    },
    {
      name: 'data.list',
      type: 'array',
      description: '数据列表'
    },
    {
      name: 'data.pagination',
      type: 'object',
      description: '分页信息'
    },
    {
      name: 'data.pagination.page',
      type: 'number',
      description: '当前页码'
    },
    {
      name: 'data.pagination.pageSize',
      type: 'number',
      description: '每页条数'
    },
    {
      name: 'data.pagination.total',
      type: 'number',
      description: '总条数'
    }
  ]
}

// 文件上传接口模板
const fileUploadTemplate = {
  method: 'POST',
  url: '/api/files/upload',
  parameters: [
    {
      name: 'file',
      type: 'file',
      required: true,
      description: '要上传的文件'
    },
    {
      name: 'description',
      type: 'string',
      required: false,
      description: '文件描述'
    }
  ],
  response: [
    {
      name: 'code',
      type: 'number',
      description: '状态码'
    },
    {
      name: 'message',
      type: 'string',
      description: '响应消息'
    },
    {
      name: 'data',
      type: 'object',
      description: '响应数据'
    },
    {
      name: 'data.id',
      type: 'number',
      description: '文件ID'
    },
    {
      name: 'data.url',
      type: 'string',
      description: '文件访问地址'
    },
    {
      name: 'data.name',
      type: 'string',
      description: '文件名'
    },
    {
      name: 'data.size',
      type: 'number',
      description: '文件大小'
    }
  ]
}

// 模板列表
export const apiTemplates = [
  {
    id: 'login',
    name: '登录接口',
    template: loginTemplate
  },
  {
    id: 'paginatedList',
    name: '分页列表',
    template: paginatedListTemplate
  },
  {
    id: 'fileUpload',
    name: '文件上传',
    template: fileUploadTemplate
  }
]

// 根据ID获取模板
export function getTemplateById(id) {
  const template = apiTemplates.find(t => t.id === id)
  return template ? JSON.parse(JSON.stringify(template.template)) : null
}

export default {
  apiTemplates,
  getTemplateById
}